#include "schedulemodel.h"
#include "userscheduledata.h"
#include <QPushButton>

#include <QDebug>

ScheduleModel::ScheduleModel() : QAbstractTableModel(), currentSchedule_(NULL) {
}

ScheduleModel::~ScheduleModel() {
    currentSchedule_ = NULL;
    foreach(QString key, schedules_.keys()) {
        delete schedules_[key];
        schedules_[key] = NULL;
    }

    schedules_.clear();
}

int ScheduleModel::rowCount(const QModelIndex &parent) const {
    if(currentSchedule_) {
        return currentSchedule_->getUsersNumber();
    } else {
        return 0;
    }
}

int ScheduleModel::columnCount(const QModelIndex &parent) const {
    if(currentSchedule_) {
        return currentSchedule_->getShiftsNumber();
    } else {
        return 0;
    }
}

QVariant ScheduleModel::data(const QModelIndex &index, int role) const {
    if(currentSchedule_) {
        if(role == Qt::DisplayRole) {
            switch(currentSchedule_->getShiftType(index.row(), index.column())) {
            case DAYOFF:
                return "";
                break;
            case DAILY:
                return "D";
                break;
            case NIGHTLY:
                return "N";
                break;
            case UNAVAILABLE:
                return "/";
                break;
            }
        } else if(role == Qt::TextAlignmentRole) {
            return Qt::AlignCenter;
        }
    }

    return QVariant();
}

Qt::ItemFlags ScheduleModel::flags(const QModelIndex & index) const {
    if(index.isValid()) {
        return Qt::ItemIsEnabled;// | QAbstractItemModel::flags(index);
    } else {
        return QAbstractItemModel::flags(index);
    }
}

QVariant ScheduleModel::headerData(int section, Qt::Orientation orientation,
                                   int role) const
{
    if(currentSchedule_ && orientation == Qt::Horizontal) {
        if(role == Qt::DisplayRole) {
            return currentSchedule_->getLabel(section + 1);
        }
    } else if(currentSchedule_ && orientation == Qt::Vertical) {
        if(role == Qt::DisplayRole) {
            if(currentSchedule_->getUsersNumber()) {
                return currentSchedule_->getUser(section);
            }
        }
    }

    return QAbstractTableModel::headerData(section, orientation, role);
}

void ScheduleModel::addUser(const QString &name) {
    if(currentSchedule_) {
        if(!currentSchedule_->userExists(name)) {
            beginInsertRows(QModelIndex(), 0, 0);
            currentSchedule_->addUser(name);
            endInsertRows();
        }
    }
}

void ScheduleModel::switchShiftType(const QModelIndex &index) {
    if(currentSchedule_) {
        currentSchedule_->switchShiftType(index.row(), index.column());
    }

    emit dataChanged(index, index);
}

void ScheduleModel::loadSchedule(const QDate &date) {
    beginRemoveRows(QModelIndex(), 0, rowCount());
    removeRows(0, rowCount());
    endRemoveRows();

    beginRemoveColumns(QModelIndex(), 0, columnCount());
    removeColumns(0, columnCount());
    endRemoveColumns();

    beginResetModel();
    reset();
    endResetModel();

    QString hash = Schedule::hash(date.month(), date.year());

    if(schedules_.contains(hash)) {
        currentSchedule_ = schedules_[hash];
    } else {
        currentSchedule_ = new Schedule(date);
        schedules_[hash] = currentSchedule_;
    }

    beginInsertColumns(QModelIndex(), 0, currentSchedule_->getShiftsNumber() - 1);
    endInsertColumns();
}
